@extends('admin.layout.layout', ['row' => @$page])

@section('title', @$page['title'])

@section('contain')
    <div class="container" style="padding-bottom: 20px; padding-top: 20px;">
        <div class="row" style="text-align: center;margin-bottom: 20px;">
            <div class="col"><h2>{{ $title }}</h2></div>
            <div class="col-sm-1" style="float: right;">
                <a href="/admin/stat/roll"><button class="btn btn-primary">返回</button></a>
            </div>
        </div>
        <div class="row select-area" style="margin-top:10px;">
            <div class="col-sm-1">
                <button type="button" class="btn {{ !empty($dist_id) ? 'btn-primary' : 'btn-secondary' }} " id="all_country">全国</button>
            </div>
            <div class="col-md-3">
                <div class="input-group mb-3 custom-control-inline">
                    <div class="input-group-prepend">
                        <span class="input-group-text">省代</span>
                    </div>
                    <select class="form-control" style="width: 70%" id="distributions">
                        <option selected disabled value="">选择省代</option>
                        @if (!empty($distributions))
                            @foreach($distributions as $distribution)
                                <option value="{{ $distribution->id }}" {{ $dist_id == $distribution->id ? 'selected' : '' }}>{{ $distribution->abbr }}</option>
                            @endforeach
                        @endif
                    </select>
                </div>
            </div>
            <div class="col-md-3">
                <div class="input-group mb-3 custom-control-inline">
                    <div class="input-group-prepend">
                        <span class="input-group-text">经销商</span>
                    </div>
                    <select class="form-control" style="width: 70%" id="dealers">
                        <option selected disabled value="">选择经销商</option>
                        @if (!empty($dealers))
                            @foreach($dealers as $dealer)
                                <option value="{{ $dealer->id }}" {{ $dealer_id == $dealer->id ? 'selected' : '' }}>{{ $dealer->abbr }}</option>
                            @endforeach
                        @endif
                    </select>
                </div>
            </div>
        </div>
        <div class="row" style="margin-top:10px;">
            <div class="col-sm-2 form-group">
                <select class="form-control" style="width: 60%;" id="year">
                    @for ($i = $start_year; $i > $start_year - 4; $i--)
                        <option value="{{ $i }}" {{ $i == $year ? 'selected' : '' }}>{{ $i }}</option>
                    @endfor
                </select>
            </div>
            <div class="col-sm-2 form-group">
                <select class="form-control" style="width: 60%;" id="old_year">
                    @for ($i = $start_year; $i > $start_year - 4; $i--)
                        <option value="{{ $i }}" {{ $i == $old_year ? 'selected' : '' }}>{{ $i }}</option>
                    @endfor
                </select>
            </div>
        </div>
        <div class="row">
            <table class="table table-bordered">
                <thead>
                    <tr class="active" style="background-color: rgb(248, 248, 248);">
                        <th>月份</th>
                        <th>{{ $year }}销售量</th>
                        <th>{{ $old_year }}销售量</th>
                        <th>{{ $year }}调货量</th>
                        <th>{{ $old_year }}调货量</th>
                        <th>{{ $year }}退货量</th>
                        <th>{{ $old_year }}退货量</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($months as $key=> $month)
                        <tr>
                        <td>{{ $month }}</td>
                        <td>{{ !empty($new_data[$key]) && !empty($new_data[$key]->sale_sum) ? $new_data[$key]->sale_sum : 0 }}</td>
                        <td>{{ !empty($old_data[$key]) && !empty($old_data[$key]->sale_sum) ? $old_data[$key]->sale_sum : 0 }}</td>
                        <td>{{ !empty($new_data[$key]) && !empty($new_data[$key]->transfer_sum) ?$new_data[$key]->transfer_sum : 0 }}</td>
                        <td>{{ !empty($old_data[$key]) && !empty($old_data[$key]->transfer_sum) ? $old_data[$key]->transfer_sum : 0 }}</td>
                        <td>{{ !empty($new_data[$key]) && !empty($new_data[$key]->return_sum) ? $new_data[$key]->return_sum : 0 }}</td>
                        <td>{{ !empty($old_data[$key]) && !empty($old_data[$key]->return_sum) ? $old_data[$key]->return_sum : 0 }}</td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
@endsection
@section('script')
    <script type="text/javascript">
        $("#year").on("change", function () {
           getRollStat();
        });
        $("#old_year").on("change", function () {
            getRollStat();
        });
        $("#distributions").on("change", function () {
            if ($("#distributions").val() == '' || typeof $("#distributions").val() == 'undefined' || $("#distributions").val() == null) {
                return;
            }
            $("#dealers").val('');
            $("#all_country").removeClass('btn-secondary').addClass('btn-primary');
            request.post('/admin/ajax/getDealers', {
                creator_id: $("#distributions").val(),
                t: 1,
            }).then(function (d) {
                console.log(d);
                let data = d.data;
                $("#dealers").empty().append('<option selected disabled value="">选择经销商</option>');
                if (data != '' && data.length > 0) {
                    for (let i in data) {
                        $("#dealers").append('<option value="' + data[i]['value'] + '">'+ data[i]['label'] +'</option>');
                    }
                }
            });
            getRollStat();
        });

        $("#dealers").on("change", function () {
            getRollStat();
        });

        $("#all_country").on('click', function () {
            $("#distributions").val('').trigger('change');
            $("#dealers").val('').empty().append('<option selected disabled value="">选择经销商</option>');
            $(this).removeClass('btn-primary').addClass('btn-secondary');
            getRollStat();
        });

        function getRollStat() {
            let url = '/admin/stat_compare/compare_qm_rolls?';
            let year = $("#year").val();
            let old_year = $("#old_year").val();
            let dist_id = $("#distributions").val();
            let dealer_id = $("#dealers").val();
            if (year != old_year && year != '' && old_year != '') {
                url += 'year=' + year + '&old_year=' + old_year;
            }
            if (dist_id != '' && typeof dist_id != 'undefined' && dist_id != null) {
                url += '&dist_id=' + dist_id;
            }
            if (dealer_id && typeof dealer_id != 'undefined' && dealer_id != null) {
                url += '&dealer_id=' + dealer_id;
            }

            if (url == '/admin/stat_compare/compare_qm_rolls?') {
                return;
            }

            location.href = url;
        }

    </script>
@endsection